package com.choosefine.base.shixiaobao.common.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.choosefine.base.shixiaobao.common.def.CommonConstant;

/**
 * 工具帮助类
 * 
 * @author liaozhanggen 2017年1月9日
 */
public class ExcelUtils {
	
	private static Logger logger = LoggerFactory.getLogger(ExcelUtils.class);
	
	/**
	 * 将文件流生成文件到指定的目录中
	 * 
	 * @author liaozhanggen 2017年1月10日
	 */
	public static void writeFileToDirectory(InputStream iStream, String fileName) throws IOException, FileNotFoundException {
		mkdirFile(fileName); //生成目录
		InputStream is = iStream;
		FileOutputStream fos = new FileOutputStream(fileName);
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = is.read(buffer)) != -1) {
			fos.write(buffer, 0, len);
		}
		is.close();
		fos.close();
	}
	
	
	/**
	 * 根据文件路径，创建文件目
	 * 
	 * @author liaozhanggen 2017年1月9日
	 */
	public static void mkdirFile(String fullFileName) {
		int indexOf = fullFileName.lastIndexOf("/");
		File file = new File(fullFileName.substring(0,indexOf));
		if(!file.exists()){
			file.mkdirs();
		}
	}
	
	/**
	 * 得到当前时间
	 * 
	 * @author liaozhanggen 2017年1月9日
	 */
	public static String getCurrentDate(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = sdf.format(new Date());
		return dateStr;
	}
	
	/**
	 * 得到当前时间
	 * 
	 * @author liaozhanggen 2017年1月9日
	 */
	public static String getCurrentTimeMillis(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmSS");
		String dateStr = sdf.format(new Date());
		return dateStr;
	}
	
	/**
	 * 生成文件流
	 * 
	 * @author liaozhanggen 2017年1月9日
	 */
	public static void generateFileStream(String fullFileName, HttpServletResponse response)
			throws UnsupportedEncodingException, IOException, FileNotFoundException {
		logger.info("-------generateFileStream------------->" + fullFileName);
		String fileName = null;
		//另存为的Excel的文件名
		int indexOf = fullFileName.lastIndexOf("/");
		if (indexOf != -1) {
			fileName = fullFileName.substring(indexOf + 1, fullFileName.length());
		}
		//下载机器码文件
		response.setHeader("conent-type", "application/octet-stream");
		response.setContentType("application/octet-stream");
		response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("ISO-8859-1"), "UTF-8"));
		
		OutputStream os = response.getOutputStream();
		BufferedOutputStream bos = new BufferedOutputStream(os);
		
		InputStream is = new FileInputStream(CommonConstant.FILE_DIRECTORY_IMPORT_PATH.concat(fullFileName));
		BufferedInputStream bis = new BufferedInputStream(is);

		int length = 0;
		byte[] temp = new byte[1 * 1024 * 10];

		while ((length = bis.read(temp)) != -1) {
			bos.write(temp, 0, length);
		}
		bos.flush();
		bis.close();
		bos.close();
		is.close();
	}
}
